<template>
  <div class="student-submission-history">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <span class="breadcrumb-item">作业提交</span>
      <span class="breadcrumb-separator">/</span>
      <span class="breadcrumb-item current">提交历史</span>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">我的提交历史</h1>
      </div>

      <!-- 学科分类标签 -->
      <div class="subject-tabs">
        <div 
          v-for="subject in subjects" 
          :key="subject.key"
          class="subject-tab"
          :class="{ active: activeSubject === subject.key }"
          @click="setActiveSubject(subject.key)"
        >
          {{ subject.label }}
        </div>
      </div>

      <!-- 状态分类标签 -->
      <div class="type-tabs">
        <div 
          v-for="status in statuses" 
          :key="status.key"
          class="type-tab"
          :class="{ active: activeStatus === status.key }"
          @click="setActiveStatus(status.key)"
        >
          {{ status.label }}
        </div>
        
        <!-- 搜索框 -->
        <div class="search-box">
          <input 
            type="text" 
            placeholder="搜索作业名称"
            v-model="searchKeyword"
            class="search-input"
          />
          <el-icon class="search-icon">
            <Search />
          </el-icon>
        </div>
      </div>

      <!-- 记录列表 -->
      <div class="records-grid">
        <StudentSubmissionCard
          v-for="record in filteredRecords"
          :key="record.id"
          :record="record"
          @detail="handleRecordDetail"
          @resubmit="handleResubmit"
        />
      </div>

      <!-- 空状态 -->
      <div v-if="filteredRecords.length === 0" class="empty-state">
        <div class="empty-icon">📝</div>
        <p class="empty-text">暂无提交记录</p>
        <p class="empty-desc">完成作业后，提交记录会显示在这里</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import StudentSubmissionCard from '@/components/student/Cards/StudentSubmissionCard.vue'

const router = useRouter()

// 学科分类数据
const subjects = [
  { key: 'all', label: '全部' },
  { key: 'math', label: '数学' },
  { key: 'english', label: '英语' },
  { key: 'chinese', label: '语文' },
  { key: 'other', label: '其他' }
]

// 状态分类数据 - 学生视角
const statuses = [
  { key: 'all', label: '全部' },
  { key: 'submitted', label: '已提交' },
  { key: 'graded', label: '已批改' },
  { key: 'correction', label: '需订正' },
  { key: 'corrected', label: '已订正' },
  { key: 'completed', label: '已完成' }
]

// 当前选中的分类
const activeSubject = ref('all')
const activeStatus = ref('all')
const searchKeyword = ref('')

// 模拟学生提交记录数据
const records = ref([
  {
    id: 1,
    title: '数学作业 - 第三章练习',
    taskName: '数学期中测试',
    subject: 'math',
    status: 'graded',
    submitTime: new Date('2024-01-15 16:30:00'),
    gradeTime: new Date('2024-01-16 09:15:00'),
    score: 85,
    totalScore: 100,
    feedback: '计算准确，但需要注意书写规范',
    hasErrors: true,
    errorCount: 2
  },
  {
    id: 2,
    title: '英语作业 - 单词练习',
    taskName: '英语单词测试',
    subject: 'english',
    status: 'completed',
    submitTime: new Date('2024-01-14 18:20:00'),
    gradeTime: new Date('2024-01-15 10:30:00'),
    score: 92,
    totalScore: 100,
    feedback: '单词掌握很好，继续保持',
    hasErrors: false,
    errorCount: 0
  },
  {
    id: 3,
    title: '语文作业 - 阅读理解',
    taskName: '语文阅读练习',
    subject: 'chinese',
    status: 'correction',
    submitTime: new Date('2024-01-13 17:45:00'),
    gradeTime: new Date('2024-01-14 11:20:00'),
    score: 78,
    totalScore: 100,
    feedback: '理解能力不错，但表达需要更准确',
    hasErrors: true,
    errorCount: 3
  },
  {
    id: 4,
    title: '数学作业 - 应用题练习',
    taskName: '数学应用题',
    subject: 'math',
    status: 'submitted',
    submitTime: new Date('2024-01-16 19:10:00'),
    gradeTime: null,
    score: null,
    totalScore: 100,
    feedback: null,
    hasErrors: false,
    errorCount: 0
  },
  {
    id: 5,
    title: '英语作业 - 语法练习',
    taskName: '英语语法测试',
    subject: 'english',
    status: 'corrected',
    submitTime: new Date('2024-01-12 16:00:00'),
    gradeTime: new Date('2024-01-13 09:45:00'),
    score: 88,
    totalScore: 100,
    feedback: '语法掌握较好，订正及时',
    hasErrors: false,
    errorCount: 0
  }
])

// 根据分类和搜索关键词筛选记录
const filteredRecords = computed(() => {
  let filtered = records.value

  // 按学科筛选
  if (activeSubject.value !== 'all') {
    filtered = filtered.filter(record => record.subject === activeSubject.value)
  }

  // 按状态筛选
  if (activeStatus.value !== 'all') {
    filtered = filtered.filter(record => record.status === activeStatus.value)
  }

  // 按搜索关键词筛选
  if (searchKeyword.value.trim()) {
    filtered = filtered.filter(record => 
      record.title.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      record.taskName.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }

  // 按提交时间倒序排列
  return filtered.sort((a, b) => b.submitTime.getTime() - a.submitTime.getTime())
})

// 设置活跃学科
const setActiveSubject = (subject: string) => {
  activeSubject.value = subject
}

// 设置活跃状态
const setActiveStatus = (status: string) => {
  activeStatus.value = status
}

// 记录操作事件
const handleRecordDetail = (record: any) => {
  console.log('查看详情:', record)
  // 跳转到作业详情页面
  router.push({
    name: 'student-homework-detail',
    params: { id: record.id },
    query: {
      taskId: record.taskId,
      context: 'submission-history'
    }
  })
}

const handleResubmit = (record: any) => {
  console.log('重新提交:', record)
  // 跳转到拍照页面重新提交作业
  router.push({
    name: 'camera-capture',
    query: {
      context: 'resubmit-homework',
      taskId: record.taskId,
      submissionId: record.id,
      returnTo: 'student-submission-history'
    }
  })
}
</script>

<style scoped>
.student-submission-history {
  min-height: 100vh;
  background-color: #f8f9fa;
}

/* 面包屑导航 */
.breadcrumb {
  padding: 16px 24px;
  font-size: 14px;
  color: #606266;
}

.breadcrumb-item {
  color: #606266;
}

.breadcrumb-item.current {
  color: #303133;
}

.breadcrumb-separator {
  margin: 0 8px;
  color: #c0c4cc;
}

/* 主内容区域 */
.main-content {
  background: #fff;
  margin: 0 24px;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

/* 页面标题 */
.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

/* 学科分类标签 */
.subject-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.subject-tab {
  padding: 12px 24px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
}

.subject-tab.active {
  color: #409eff;
}

.subject-tab.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background: #409eff;
}

.subject-tab:hover:not(.active) {
  color: #303133;
}

/* 状态分类标签 */
.type-tabs {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.type-tab {
  padding: 6px 16px;
  font-size: 14px;
  color: #606266;
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.type-tab.active {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
}

.type-tab:hover:not(.active) {
  color: #303133;
  border-color: #c0c4cc;
}

/* 搜索框 */
.search-box {
  position: relative;
  margin-left: auto;
  width: 240px;
}

.search-input {
  width: 100%;
  padding: 8px 32px 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  border-color: #409eff;
}

.search-icon {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 16px;
}

/* 记录卡片网格 */
.records-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #909399;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  font-weight: 500;
  color: #606266;
  margin: 0 0 8px 0;
}

.empty-desc {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .student-submission-history {
    padding: 0;
  }

  .breadcrumb {
    padding: 12px 16px;
  }

  .main-content {
    margin: 0 16px;
    padding: 16px;
  }

  .subject-tabs {
    flex-wrap: wrap;
  }

  .type-tabs {
    flex-wrap: wrap;
    gap: 12px;
  }

  .search-box {
    width: 100%;
    margin-left: 0;
    margin-top: 12px;
  }

  .records-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 18px;
  }

  .subject-tab {
    padding: 10px 16px;
    font-size: 13px;
  }

  .type-tab {
    padding: 4px 12px;
    font-size: 13px;
  }
}
</style>
